<?php
	session_start();
    if($_SESSION['uid']==""||$_SESSION['level']!=3){
		header("Location: ./exit.php");
		exit();
	}
	include('/php/tool/submitValidate.php');
	include('/php/tool/pdo.php');
	include('/php/class/apartment.class.php');
	include('/php/class/dormitory-allocating.class.php');
	include('/php/class/public-data.class.php');
	include('/php/bll/PublicDataBLL.php');
	include('/php/bll/DormitoryAllocatingBLL.php');

	//select 0-全部、1-已分配
	$uid = isset($_GET['uid']) ? $_GET['uid'] : '';
	$grade = isset($_GET['grade']) ? $_GET['grade'] : 0;
	$major = isset($_GET['major']) ? $_GET['major'] : 0;
	$select = isset($_GET['select']) ? $_GET['select'] : 0;
	$property = isset($_GET['property']) ? $_GET['property'] : 0;
	$apartmentID= isset($_GET['apartmentID']) ? $_GET['apartmentID'] : 0;
	$floor=isset($_GET['floor']) ? $_GET['floor'] : 0;
	$page = isset($_GET['page']) ? $_GET['page'] : 0;
	$pageSize = isset($_GET['pageSize']) ? $_GET['pageSize'] : 20;
	$search = isset($_GET['search']) ? $_GET['search'] : '';

	/*$apartmentBLL=new ApartmentBLL();
	if(!$apartmentBLL->apartmentListViewValidate($page,$pageSize,$property)){
		header("Location: http://www.baidu.com");
		exit();
	}*/
	$publicDataBLL=new PublicDataBLL();
	$dormitoryAllocatingBLL=new DormitoryAllocatingBLL();
	$display=false;
	$pageMax=0;
	switch((int)$select){
		//查询所有
		case 0:
			if($search==''){
				$dormitoryList=$dormitoryAllocatingBLL->getApartmentDormitoryList($apartmentID,$floor,$page,$pageSize);
				$pageMax=$dormitoryAllocatingBLL->getApartmentDormitoryListPage($apartmentID,$floor,$pageSize);
			}else{//search
				$dormitoryList=$dormitoryAllocatingBLL->getSearchList($select,$uid,$grade,$major,$property,$apartmentID,$floor,$search,$page,$pageSize);
				$pageMax=$dormitoryAllocatingBLL->getSearchListPage($select,$uid,$grade,$major,$property,$apartmentID,$floor,$search,$pageSize);
			}
			$display=true;
			break;
		//查询已分配
		case 1:
			if($search==''){
				$dormitoryList=$dormitoryAllocatingBLL->getInstructorDormitoryList($uid,$grade,$major,$property,$apartmentID,$floor,$page,$pageSize);
				$pageMax=$dormitoryAllocatingBLL->getInstructorDormitoryListPage($uid,$grade,$major,$property,$apartmentID,$floor,$pageSize);
			}else{//search
				$dormitoryList=$dormitoryAllocatingBLL->getSearchList($select,$uid,$grade,$major,$property,$apartmentID,$floor,$search,$page,$pageSize);
				$pageMax=$dormitoryAllocatingBLL->getSearchListPage($select,$uid,$grade,$major,$property,$apartmentID,$floor,$search,$pageSize);
			}
			$display=true;
			break;
		default:break;
	}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<!--[if lt IE 9]>
<script type="text/javascript" src="lib/html5shiv.js"></script>
<script type="text/javascript" src="lib/respond.min.js"></script>
<![endif]-->
<link rel="stylesheet" type="text/css" href="static/h-ui/css/H-ui.min.css" />
<link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/H-ui.admin.css" />
<link rel="stylesheet" type="text/css" href="lib/Hui-iconfont/1.0.8/iconfont.css" />
<link rel="stylesheet" type="text/css" href="static/h-ui.admin/skin/default/skin.css" id="skin" />
<link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/style.css" />
<!--[if IE 6]>
<script type="text/javascript" src="lib/DD_belatedPNG_0.0.8a-min.js" ></script>
<script>DD_belatedPNG.fix('*');</script>
<![endif]-->
<title>辅导员管理宿舍分配</title>
</head>
<body>
<nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 公寓管理 <span class="c-gray en">&gt;</span> 辅导员管理宿舍分配 <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新" ><i class="Hui-iconfont">&#xe68f;</i></a></nav>
<div class="page-container">
	<div class="text-c">
		<div class="cl pd-5 mybg-2 bk-gray">
			<span class="l">
				<button onclick="allocating_dormitory_checkbox()" class="btn btn-primary radius">分配选中</button>
				<button onclick="dormitory_remove_checkbox()" class="btn btn-danger radius">移除选中</button>
			</span>

			<span class="r">
				<button onclick="removeIframe()" class="btn btn-primary radius">关闭选项卡</button>
			</span>
		</div>
	</div>
	<div class="mt-10">
		<form action="instructor-dormitory-allocating.php" method="get" class="form-horizontal mybg-2 bk-gray pt-5">
			<div class="row cl mb-5">
				<label class="form-label col-xs-1" style="width:90px;margin:0;padding:0;line-height:31px;">用户名：</label>
				<div class="formControls col-xs-8 col-sm-3">
					<input id="uid" name="uid" type="text"  readonly = "readonly" value="<?php echo $uid;?>" class="input-text">
					<input id= "grade" name="grade" type="text" value="<?php echo $grade;?>" style="display:none" class="input-text">
					<input id="major" name="major" type="text" value="<?php echo $major;?>" style="display:none" class="input-text">
				</div>
				<label class="form-label col-xs-1" style="width:90px;margin:0;padding:0;line-height:31px;">查看：</label>
				<div class="formControls col-xs-8 col-sm-3">
					<select id="select_select" name="select" class="select" size="1" style="height:31px">
						<option value="0" <?php echo $select==0?'selected="selected"':'';?>>所有</option>
						<option value="1" <?php echo $select==1?'selected="selected"':'';?>>已分配</option>
					</select>
				</div>
			</div>
			<div class="row cl mb-5">
				<label class="form-label col-xs-1" style="width:90px;margin:0;padding:0;line-height:31px;">公寓分类：</label>
				<div class="formControls col-xs-8 col-sm-3">
					<select id="property_select" name="property" class="select" size="1" style="height:31px">
						<option value="0" <?php echo $property==0?'selected="selected"':'';?>>所有</option>
						<option value="1" <?php echo $property==1?'selected="selected"':'';?>>女生宿舍楼</option>
						<option value="2" <?php echo $property==2?'selected="selected"':'';?>>男生宿舍楼</option>
						<option value="3" <?php echo $property==3?'selected="selected"':'';?>>混合宿舍楼</option>
					</select>
				</div>
				<label class="form-label col-xs-1" style="width:90px;margin:0;padding:0;line-height:31px;">公寓：</label>
				<div class="formControls col-xs-8 col-sm-3">
					<select id="apartment_select" name="apartmentID" class="select" size="1" style="height:31px">
						<?php 
							if($select==1){
								echo '<option value="0">所有</option>';
							}
							$apartmentList=$publicDataBLL->getApartmentList($property);
							foreach($apartmentList as $apartment){
								if($apartment['apartment_id']==$apartmentID){
									echo '<option value="'.$apartment['apartment_id'].'" selected="selected">'.$apartment['apartment_name'].'</option>';
								}else{
									echo '<option value="'.$apartment['apartment_id'].'">'.$apartment['apartment_name'].'</option>';
								}
								
							}
						?>
					</select>

				</div>
			</div>
			
			<div class="row cl mb-5">
				<label class="form-label col-xs-1" style="width:90px;margin:0;padding:0;line-height:31px;">楼层：</label>
				<div class="formControls col-xs-8 col-sm-3">
					<select id="floor_select" name="floor" class="select" size="1" style="height:31px">
						<?php 
							echo '<option value="0">所有</option>';
							if(!$apartmentID==0){
								for($i=1;$i<=$apartmentList[0]['floor_number'];$i++){
									if($i==$floor){
											echo '<option value="'.$i.'" selected="selected">'.$i.'</option>';
									}else{
										echo '<option value="'.$i.'">'.$i.'</option>';
									}
								}
							}
						?>
					</select>
				</div>
				<label class="form-label col-xs-1" style="width:90px;margin:0;padding:0;line-height:31px;">宿舍编号：</label>
				<div class="formControls col-xs-8 col-sm-3">
					<input name="search" type="text" value="<?php echo $search;?>" class="input-text">
				</div>
				<button id="btn-search" class="btn btn-success" type="submit"><i class="Hui-iconfont">&#xe665;</i> 查找</button>
			</div>
		</form>
	</div>

	<div class="mt-10" style="display:flex;justify-content:flex-start;flex-wrap:wrap;">
	<table class="table table-border table-bordered table-hover table-bg">
		<thead>
			<tr>
				<th scope="col" colspan="10">宿舍分配</th>
			</tr>
			<tr class="text-c">
				<th width="25"><input type="checkbox" value="" name=""></th>
				<th width="130">公寓名称</th>
				<th width="100">楼层</th>
				<th width="100">宿舍分类</th>
				<th width="100">宿舍编号</th>
				<th width="100">总床位数</th>
				<th width="100">已分配</th>
				<th width="100">可分配床位</th>
				<th while="100">分配数量</th>
				<th width="50">操作</th>
			</tr>
		</thead>
		<tbody>
			
			<?php 
				
				if($display){
					foreach($dormitoryList as $dormitory){
						//获取该宿舍已经分配出去的床位
						$alreadyUsedQuantity=$dormitoryAllocatingBLL->getBedsAlreadyUsedQuantity($dormitory['apartment_id'],$dormitory['floor'],$dormitory['dormitory_id'],$grade,$major);
						//获取该辅导员在该宿舍已经分配的床位
						$instrutorAlreadyUsedQuantity=(int)$select==0?$dormitoryAllocatingBLL->getInstructorBedsAlreadyUsedQuantity($dormitory['apartment_id'],$dormitory['floor'],$dormitory['dormitory_id'],$uid,$grade,$major):$dormitory['allocated_beds'];
						//$instrutorAlreadyUsedQuantity=$dormitoryAllocatingBLL->getInstructorBedsAlreadyUsedQuantity($dormitory['apartment_id'],$dormitory['floor'],$dormitory['dormitory_id'],$uid,$grade,$major);
						//可分配床位数量，辅导员已获得的床位数加上未分配的床位数（总床位数-已分配+辅导员已获得）
						$number=(int)$dormitory['beds'];
						$availableQuantity=$number-$alreadyUsedQuantity+$instrutorAlreadyUsedQuantity;	
						
						echo '<tr class="text-c">
						<td><input class="dormitory_id" type="checkbox" value="'.$dormitory['dormitory_id'].'" name="dormitory_allocating"></td>
						<td style="display:none"><input class="apartment_id" type="text" value="'.$dormitory['apartment_id'].' class="input-text"></td>
						<td class="apartment_name">'.$dormitory['apartment_name'].'</td>
						<td class="floor">'.$dormitory['floor'].'</td>
						<td class="propety">';
						switch((int)$dormitory['dormitory_property']){
							case 1:
								echo '女生宿舍';
								break;
							case 2:
								echo '男生宿舍';
								break;
							case 3:
								echo '未确定';
								break;
							default:echo '未确定';break;
						}
						echo '</td>
						<td class="dormitory_id">'.$dormitory['dormitory_id'].'</td>
						<td class="beds_total">'.$dormitory['beds'].'</td>
						<td class="instrutor_already_used_quantity">'.$instrutorAlreadyUsedQuantity.'</td>
						<td class="available_quantity">'.$availableQuantity.'</td>
						<td style="padding:0;line-heightt:31px;"><select name="floor" class="select beds_select" size="1" style="height:31px;border:none">';
						//可分配数量
						for($i=1;$i<=$availableQuantity;$i++){
							if($i===$instrutorAlreadyUsedQuantity){
								echo '<option selected="selected" value="'.$i.'">'.$i.'</option>';
							}else{
								echo '<option value="'.$i.'">'.$i.'</option>';
							}
							
						}
						echo '</select></td><td class="f-14"><a title="保存" href="javascript:;" onclick="allocating_dormitory(this,'.$dormitory['apartment_id'].')" style="text-decoration:none"><i class="Hui-iconfont">&#xe632;</i></a> 
						<a title="清除分配" href="javascript:;" onclick="dormitory_remove(this)" class="ml-5" style="text-decoration:none">
						<i class="Hui-iconfont">&#xe6e2;</i></a></td></tr>';
					}
				}

			?>
		</tbody>
	</table>
	</div>
	<div class="modal-footer">
		<a class="btn" href="<?php echo './instructor-dormitory-allocating.php?uid='.$uid.'&major='.$major.'&grade='.$grade.'&select='.$select.'&property='.$property.'&apartmentID='.$apartmentID.'&floor='.$floor.'&search='.$search.'&page='.($page-1>=0?$page-1:0).'&pageSize='.$pageSize; ?>">上一页</a>
			<span><select  id="allocating_page" name="page" class="btn">
				<?php
					if($pageMax>0){
						for($i=0;$i<$pageMax;$i++){
							if($i==$page){//selected=""
								echo '<option value="'.$i.'" selected="selected">'.($i+1).'</option>';
							}else{
								echo '<option value="'.$i.'" ">'.($i+1).'</option>';
							}
						}
					}else{
						echo '<option value="0">1</option>';
					}
				?>
				<!--<option value="0" selected="">1</option>
				<option value="1">2</option>
				<option value="2">3</option>-->
			</select>
		</span>
		<a class="btn" href="<?php echo './instructor-dormitory-allocating.php?uid='.$uid.'&major='.$major.'&grade='.$grade.'&select='.$select.'&property='.$property.'&apartmentID='.$apartmentID.'&floor='.$floor.'&search='.$search.'&page='.($page+1<$pageMax?$page+1:$page).'&pageSize='.$pageSize; ?>">下一页</a>
	</div>
</div>
<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script> 
<script type="text/javascript" src="lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="static/h-ui/js/H-ui.min.js"></script> 
<script type="text/javascript" src="static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script>
	var apartmentList='<?php 
	$apartmentList=$publicDataBLL->getApartmentList();
	foreach($apartmentList as $apartment){
		echo '<option value="'.$apartment['apartment_id'].'">'.$apartment['apartment_name'].'</option>';
	}
	?>';
	var apartmentFloor=[
	<?php 
		for($i=0;$i<count($apartmentList);$i++){
			echo '{id:"'.$apartmentList[$i]['apartment_id'].'",apartment_name:"'.$apartmentList[$i]['apartment_name'].'",property:"'.$apartmentList[$i]['property'].'",floor:"'.$apartmentList[$i]['floor_number'].'"}';echo $i<count($apartmentList)-1?',':'';
		}
	?>];
</script>
<script type="text/javascript" src="scripts/instructor-dormitory-allocting.jquery.js"></script>

</body>
</html>